package com.oa.srms.apps.actions.pay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.goods.IGoodCategoryService;
import com.oa.srms.apps.business.pay.IPayInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.goods.GoodCategoryValue;
import com.oa.srms.apps.entity.pay.PayInfoValue;

@SuppressWarnings("serial")
public class PayInfoAction extends BasicAction {
	
	@Autowired
	private IPayInfoService payInfoService;
	@Autowired
	private IGoodCategoryService goodCategoryService;
	
	private List<PayInfoValue> payInfoList;
	private List<GoodCategoryValue> goodCategoryList;
	
	private String cardId;
	private Double amount;
	private String contents;
	private String str;
	private String stuNo;
	
	public String toAdd() throws Exception{
		return "toAddPage";
	}
	
	public String toQuery() throws Exception{
		return "toQueryPage";
	}
	
	public String toQueryByStu() throws Exception {
		return "toQueryByStuPage";
	}
	
	public String addPay() throws Exception {
		payInfoService.addPayInfo(cardId, amount, new Date(), contents);
		return "toAdd";
	}
	
	public String toCheckPay() throws Exception {
		goodCategoryList = goodCategoryService.quaryGoodCategoryValue(IDEDStates.ENABLE);
		return "toCheckPayPage";
	}
	
	public String queryPayInfo() throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Double total = 0.0;
		String states = null;
		String ids= "";
		str ="";
		payInfoList = payInfoService.queryPayInfo(cardId);
		if(payInfoList.size() != 0 && payInfoList != null){
			for(PayInfoValue payInfo:payInfoList){
				total += payInfo.getAmount();
				ids += payInfo.getId()+",";
				if(payInfo.getStates() == 0){
					states = "<font color='red'>已缴费</font>";
				}
				if(payInfo.getStates() == 1){
					states = "<font color='green'>已审核缴费</font>";
				}
				str+="</tr><tr><td>&nbsp;"+payInfo.getCardId()+"</td><td>&nbsp;"+payInfo.getAmount()+"</td><td>&nbsp;"+sdf.format(payInfo.getPayTime())+"</td><td>&nbsp;"+states+"</td><td colspan='4'>&nbsp;"+payInfo.getContents()+"</td></tr><tr>";
			}
			str += "||"+ total +"||"+ids.substring(0,ids.length()-1);
		}
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		return null;
	}
	
	
	// Getter and Setter

	public List<PayInfoValue> getPayInfoList() {
		return payInfoList;
	}

	public void setPayInfoList(List<PayInfoValue> payInfoList) {
		this.payInfoList = payInfoList;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}
	
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getStuNo() {
		return stuNo;
	}

	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}

	public List<GoodCategoryValue> getGoodCategoryList() {
		return goodCategoryList;
	}

	public void setGoodCategoryList(List<GoodCategoryValue> goodCategoryList) {
		this.goodCategoryList = goodCategoryList;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}
	
	
	
}
